import {createRouter, createWebHashHistory, type RouteRecordRaw} from "vue-router";
import dashboardRoutes from './modules/dashboard.ts'
import authRoutes from './modules/auth.ts'
import {isLogin} from "@/utils/auth.ts";
const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/layout/index.vue'),
    redirect: '/dashboard',
    meta: {
      title: '管理工作台',
      requiresAuth: true
    },
    children: dashboardRoutes
  },
  {
    path: '/organization/invite',
    name: 'Invite',
    component: () => import('@/views/home/invite.vue'),
    meta: {
      title: '邀请加入组织',
      requiresAuth:false
    }
  },
  authRoutes,
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404.vue'),
    meta: {
      title: '404',
      requiresAuth:  false
    }
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
const APP_TITLE = import.meta.env.VITE_APP_TITLE
const whitelist = ['/login','/register']
router.beforeEach((to, _from, next) => {
  document.title = `${to.meta.title} - ${APP_TITLE}`
  if (whitelist.includes(to.path) || isLogin()){
    next()
  }else{
    next('/login')
  }
})
export default router